home *** CD-ROM | disk | FTP | other *** search
- /*--------------------------------------------------------------------------*/
- /* */
- /* AddFrameToMovie. */
- /* by John Wang */
- /* */
- /* Description: This program demonstrates adding a frame to the end */
- /* of an existing movie. */
- /* */
- /* Version: 1.0 Completed 12/2/91 */
- /* 1.1 Fixed error where ind was assumed to be 0 based */
- /* rather than 1 based. And, added code to update */
- /* movie rather than require a flatten movie for */
- /* saving the new edited movie. */
- /* */
- /*--------------------------------------------------------------------------*/
-
- #include <GestaltEqu.h>
- #include "Movies.h"
-
- #define Gestalttest 0xA1AD
- #define NoTrap 0xA89F
-
- #define appleID 128
- #define appleMenu 0
- #define aboutMeCommand 1
-
- #define fileID 129
- #define openCommand 1
- #define flattenCommand 2
- #define closeCommand 3
- #define quitCommand 5
-
- #define aboutMeDLOG 128
- #define okButton 1
-
- #define MAXWINDOWS 5
-
- /*------------------------------------------------------*/
- /* Global Variables. */
- /*------------------------------------------------------*/
-
- Boolean DoneFlag = FALSE;
- MenuHandle mymenu0, mymenu1;
- Boolean playingMovie[MAXWINDOWS];
- Movie myMovie[MAXWINDOWS];
- WindowPtr myWindow[MAXWINDOWS];
- int startlocation;
-
- void Error(StringPtr errorMsg, OSErr result)
- {
- if (result) DebugStr(errorMsg);
- }
-
- /*------------------------------------------------------*/
- /* getMovieFromFile(). */
- /*------------------------------------------------------*/
-
- Movie getMovieFromFile()
- { OSErr err;
- StandardFileReply reply;
- Point where = {200, 50};
- SFTypeList types;
- short movieResRefNum;
- short actualResId;
- Movie theMovie;
- long i, maxCompressionSize;
- Track myVideoTrack;
- Media myVideoMedia;
- OSType myMediaType;
- Boolean done = FALSE;
- GWorldPtr myGWorld;
- Rect movieBounds;
- GDHandle oldGDevice;
- CGrafPtr oldPort;
- Handle compressedData;
- ImageDescriptionHandle imageDescH;
- TimeValue sampleDuration;
-
- types[0] = 'MooV';
- StandardGetFilePreview(nil, 1, types, &reply);
- if (!reply.sfGood) return((Movie) 0);
-
- err = OpenMovieFile(&reply.sfFile, &movieResRefNum, fsWrPerm);
- if (GetMoviesError()) return((Movie) 0);
- if (err) return ((Movie) 0);
-
- actualResId = 0;
-
- err = NewMovieFromFile(&theMovie, movieResRefNum, &actualResId, (StringPtr) 0, newMovieActive, (Boolean *) 0);
- if (GetMoviesError()) return((Movie) 0);
-
- if (err) return ((Movie) 0);
-
- /* This is where I add a frame to the end of the movie's video track. */
-
- for (i=1; ((i<=GetMovieTrackCount(theMovie)) && (!done)); i++) {
- myVideoTrack = GetMovieIndTrack(theMovie, i);
- myVideoMedia = GetTrackMedia(myVideoTrack);
- GetMediaHandlerDescription(myVideoMedia, &myMediaType, nil, nil);
- if (myMediaType == VideoMediaType) done = TRUE;
- }
- if (done == FALSE) Error("\PMovie contains no video tracks.", -1);
- else {
- if (BeginMediaEdits(myVideoMedia))
- Error("\PBeginMediaEdits failed.", -1);
-
- GetMovieBox(theMovie, &movieBounds);
- if (NewGWorld(&myGWorld, 1, &movieBounds, nil, nil, 0)) Error("\PNewGWorld failed.", -1);
- else {
-
- GetGWorld(&oldPort, &oldGDevice);
-
- SetGWorld(myGWorld, nil); /* Draw the Image... */
- LockPixels(GetGWorldPixMap(myGWorld));
- EraseRect(&movieBounds);
- MoveTo(10,10); DrawString("\PHi there, John!!!");
-
- imageDescH = (ImageDescriptionHandle) NewHandle(4);
- if (GetMaxCompressionSize(GetGWorldPixMap(myGWorld),
- &movieBounds,
- 1,
- codecNormalQuality,
- 'raw ',
- anyCodec,
- &maxCompressionSize))
- Error("\PCompressImage.", -1);
- compressedData = NewHandle(maxCompressionSize);
- if (compressedData == nil) Error("\PCould not allocate compressedData block", -1);
- else {
- MoveHHi(compressedData); HLock(compressedData);
-
- if (err = CompressImage(GetGWorldPixMap(myGWorld),
- &movieBounds,
- codecNormalQuality,
- 'raw ',
- imageDescH,
- StripAddress(*compressedData)))
- Error("\PCompressImage.", err);
-
- if (err = AddMediaSample(myVideoMedia,
- compressedData,
- 0,
- (**imageDescH).dataSize,
- GetMediaTimeScale(myVideoMedia),
- (SampleDescriptionHandle) imageDescH,
- 1,
- 0,
- &sampleDuration))
- Error("\PInsertMediaIntoTracks.", err);
-
- if (err = InsertMediaIntoTrack(myVideoTrack,
- GetTrackDuration(myVideoTrack),
- sampleDuration,
- GetMediaTimeScale(myVideoMedia),
- 0x00010000))
- Error("\PInsertMediaIntoTracks.", err);
-
- if (err = EndMediaEdits(myVideoMedia)) Error("\PEndMediaEdits failed.", err);
-
- SetGWorld(oldPort, oldGDevice);
- HUnlock(compressedData);
- DisposeHandle(compressedData);
- }
- DisposeHandle(imageDescH);
-
- DisposeGWorld(myGWorld);
- }
- }
-
- if (err = UpdateMovieResource(theMovie, movieResRefNum, actualResId, nil))
- Error("\PUpdateResource failed.", err);
-
- /* This is the end of the code for adding the one frame to the movie. */
-
- err = CloseMovieFile(movieResRefNum);
- if (GetMoviesError()) return((Movie) 0);
- if (err) return ((Movie) 0);
-
- return (theMovie);
- }
-
- /*------------------------------------------------------*/
- /* playMovie(). */
- /*------------------------------------------------------*/
-
- OSErr playMovie(int index)
- {
- Rect movieBounds;
-
- GetMovieBox(myMovie[index], &movieBounds);
- OffsetRect(&movieBounds, -movieBounds.left, -movieBounds.top);
- if (movieBounds.right < 40) movieBounds.right = 40;
- if (movieBounds.bottom < 20) movieBounds.bottom = 20;
- SetMovieBox(myMovie[index], &movieBounds);
- OffsetRect(&movieBounds, startlocation, startlocation);
- myWindow[index] = NewCWindow(0L, &movieBounds, "\PMovie!", 1, 0, (WindowPtr) -1, TRUE, 0L);
- startlocation += 50;
- if (startlocation > 300) startlocation = 50;
-
- SetMovieGWorld(myMovie[index], (CGrafPtr) myWindow[index], 0);
- if (GetMoviesError()) Error("\PSetMovieGWorld error.", GetMoviesError());
-
- /* Uncomment these lines if you want to pre load the movie into ram.
- GotoBeginningOfMovie(myMovie[index]);
- if (LoadMovieIntoRam(myMovie[index], GetMovieTime(myMovie[index], 0L),
- GetMovieDuration(myMovie[index]), 0) != noErr)
- Error("\PNot enough memory to load movie into ram.", -1);
- */
- SetMovieRate(myMovie[index], 0x00010000);
-
- }
-
- /*------------------------------------------------------*/
- /* flatten(). */
- /*------------------------------------------------------*/
-
- short flatten(Movie myMovie)
- { StandardFileReply reply;
- OSErr theErr = noErr;
-
- StandardPutFile("\PName of flattened movie.", "\PUntitled", &reply);
- if (!reply.sfGood) return;
-
- if (theErr = GetMoviesError()) Error("\PCall Before FlattenMovies failed.", theErr);
-
- FlattenMovie(myMovie,
- flattenAddMovieToDataFork,
- &reply.sfFile,
- 'JWJW',
- 0,
- createMovieFileDeleteCurFile,
- nil,
- nil);
-
- if (theErr = GetMoviesError()) Error("\PFlattenMovies failed.",theErr);
- return(theErr);
- }
-
- /*------------------------------------------------------*/
- /* doFlattenCommand(). */
- /*------------------------------------------------------*/
- void doFlattenCommand()
- { int i;
- WindowPtr myTempWindow;
-
- /* Flatten movie that is currently selected. */
- myTempWindow = FrontWindow();
- if (myTempWindow == nil) return;
-
- for (i = 0; i < MAXWINDOWS; i++)
- if (myWindow[i] == myTempWindow) {
- flatten(myMovie[i]);
- break;
- }
- }
-
- /*------------------------------------------------------*/
- /* doOpenCommand(). */
- /*------------------------------------------------------*/
- void doOpenCommand()
- { int i;
-
- /* Search for the first window that is nil. */
- for (i = MAXWINDOWS-1; i >= 0; i--)
- if (myWindow[i] == nil) {
- myMovie[useThisIndex] = getMovieFromFile();
- if (myMovie[useThisIndex] != 0) {
- playMovie(useThisIndex);
- playingMovie[useThisIndex] = TRUE;
- }
- break;
- }
- }
-
- /*------------------------------------------------------*/
- /* playMovies(). */
- /*------------------------------------------------------*/
- playMovies()
- { int i;
-
- for (i = 0; i < MAXWINDOWS; i++)
- if (playingMovie[i] == TRUE) {
- if (IsMovieDone(myMovie[i]) == FALSE)
- MoviesTask(myMovie[i], DoTheRightThing);
- }
- }
-
- /*------------------------------------------------------*/
- /* doCloseCommand(). */
- /*------------------------------------------------------*/
- void doCloseCommand()
- { int i, useThisIndex;
- WindowPtr myTempWindow;
-
- myTempWindow = FrontWindow(); /* Close selected window. */
- if (myTempWindow == nil) return;
- for (i = 0; i < MAXWINDOWS; i++)
- if (myWindow[i] == myTempWindow) {
- DisposeMovie(myMovie[i]);
- DisposeWindow(myTempWindow);
- playingMovie[i] = FALSE;
- myWindow[i] = nil;
- }
- }
-
- /*------------------------------------------------------*/
- /* doCommand(). */
- /*------------------------------------------------------*/
- void doCommand(long mResult)
- { int theMenu, theItem;
- char daName[256];
- GrafPtr savePort;
-
- theItem = LoWord(mResult); theMenu = HiWord(mResult);
- switch (theMenu) {
- case appleID:
- if (theItem == aboutMeCommand) showAboutMeDialog();
- else {
- GetItem(mymenu0, theItem, daName);
- GetPort(&savePort); (void) OpenDeskAcc(daName);
- SetPort(savePort);
- }
- break;
-
- case fileID:
- switch (theItem) {
- case openCommand: doOpenCommand(); break;
- case flattenCommand: doFlattenCommand(); break;
- case closeCommand: doCloseCommand(); break;
- case quitCommand: DoneFlag = TRUE; break;
- }
- break;
- }
- HiliteMenu(0);
- return;
- }
-
- /*------------------------------------------------------*/
- /* init(). */
- /*------------------------------------------------------*/
- void init()
- { OSErr err;
- int i;
- long QDfeature, OSfeature;
-
- /* Initialize Managaer. */
- InitGraf(&qd.thePort);
- FlushEvents(everyEvent, 0);
- InitWindows(); InitDialogs(nil); InitCursor();
-
- /* Set up menus. */
- mymenu0 = GetMenu(appleID); AddResMenu(mymenu0, 'DRVR'); InsertMenu(mymenu0,0);
- mymenu1 = GetMenu(fileID); InsertMenu(mymenu1,0);
- DrawMenuBar();
-
- /* Use Gestalt to find if QuickDraw and QuickTime is available. */
- if ((GetTrapAddress(Gestalttest) != GetTrapAddress(NoTrap))) {
- err = Gestalt(gestaltQuickdrawVersion, &QDfeature);
- if (!err) err = Gestalt(gestaltSystemVersion, &OSfeature);
- if (!err) { /* Check if Color QuickDraw & System Version 6.07 or later? */
- if ((QDfeature & 0x0f00) != 0x0200 && OSfeature < 0x0607) ExitToShell();
- err = Gestalt(gestaltQuickTime, &QDfeature);
- }
- if (err) ExitToShell();
- } else ExitToShell();
-
- startlocation = 50; /* Set up variables. */
- for (i = 0; i < MAXWINDOWS; i++) {
- playingMovie[i] = FALSE;
- myWindow[i] = nil;
- }
-
- /* Open QuickTime last. */
- err = EnterMovies();
- if (err) ExitToShell();
- }
-
- /*------------------------------------------------------*/
- /* finish(). */
- /*------------------------------------------------------*/
- void finish()
- {
- ExitMovies();
- ExitToShell();
- }
-
- /*------------------------------------------------------*/
- /* showAboutMeDialog() */
- /*------------------------------------------------------*/
- void showAboutMeDialog()
- { GrafPtr savePort;
- DialogPtr theDialog;
- short itemHit;
-
- GetPort(&savePort);
- theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
- SetPort(theDialog);
-
- do {
- ModalDialog(nil, &itemHit);
- } while (itemHit != okButton);
-
- CloseDialog(theDialog); SetPort(savePort);
- return;
- }
-
- /*------------------------------------------------------*/
- /* main(). */
- /*------------------------------------------------------*/
-
- main()
- { int i;
- char key;
- Boolean track;
- long growResult;
- EventRecord myEvent;
- WindowPtr whichWindow;
- int yieldTime;
-
- init();
-
- yieldTime = 0;
- for ( ;; ) {
-
- /* We can't just do ExitToShell because we must cann ExitMovies. */
- if (DoneFlag) finish(); /* Call's ExitToShell()! */
-
- /* Play movies which are active. */
- playMovies();
-
- if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
- switch (myEvent.what) {
- case mouseDown:
- switch (FindWindow(myEvent.where, &whichWindow)) {
- case inSysWindow: SystemClick(&myEvent, whichWindow);
- break;
- case inMenuBar: doCommand(MenuSelect(myEvent.where));
- break;
- case inContent: SelectWindow(whichWindow);
- break;
- case inDrag: DragWindow (whichWindow, myEvent.where, &qd.screenBits.bounds);
- break;
- case inGoAway:
- track = TrackGoAway (whichWindow, myEvent.where);
- if (track) doCloseCommand();
- break;
- case inGrow: case inZoomIn: case inZoomOut: break;
- }
- break;
- case keyDown:
- case autoKey:
- key = myEvent.message & charCodeMask;
- if ( myEvent.modifiers & cmdKey )
- if ( myEvent.what == keyDown ) doCommand(MenuKey(key));
- break;
- case updateEvt:
- for (i = 0; i < MAXWINDOWS; i++)
- if ((WindowPtr) myEvent.message == myWindow[i]) {
- BeginUpdate((WindowPtr) myWindow[i]);
- EndUpdate((WindowPtr) myWindow[i]);
- }
- break;
- case diskEvt: break;
- case activateEvt: break;
- case app4Evt: break;
- }
- }
- }
- }